﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Configuration

Public Class FIMUtils

    Public Class WellKnownGuids
        Public Shared BuiltInSynchronizationAccount As New Guid("fb89aefa-5ea1-47f1-8890-abe7797d6497")
        Public Shared FIMServiceAccount As New Guid("e05d1f1b-3d5e-4014-baa6-94dee7d68c89")
        Public Shared Anonymous As New Guid("b0b36673-d43b-4cfa-a7a2-aff14fd90522")
    End Class

    Public Class Debugging

        Public Sub Log(ByVal s As String)

            Dim message As String = String.Format("{0}: {1}", Now.ToString("yyyy-MM-dd hh::mm:ss"), If(String.IsNullOrEmpty(s), "(null)", s))

            Dim logFileName As String = "c:\temp\log.txt"

            Using f As New StreamWriter(logFileName, True)
                f.WriteLine(message)
            End Using

        End Sub


    End Class

    Public Class sqlLookups

        Private _sqlConnection As SqlConnection
        Private _connectionString As String

        Sub New(ByVal dbname As String)

            _connectionString = GetConnectionStringByName(dbname)

            _sqlConnection = New SqlConnection(_connectionString)

            '_sqlConnection = New SqlConnection("Data Source=APP01;Database=Utility;Integrated Security=true;")

            _sqlConnection.Open()
        End Sub

        Sub Close()
            _sqlConnection.Close()
        End Sub

        ' Retrieves a connection string by name from the app.config file. Returns Nothing if the name is not found.
        Private Shared Function GetConnectionStringByName(ByVal name As String) As String

            ' Assume failure
            Dim returnValue As String = Nothing

            ' Look for the name in the connectionStrings section.
            Dim settings As ConnectionStringSettings = _
            ConfigurationManager.ConnectionStrings(name)

            ' If found, return the connection string.
            If Not settings Is Nothing Then
                returnValue = settings.ConnectionString
            End If

            Return returnValue
        End Function


        'Takes an attribute, its value, a UID attribute and its value and tests the shadowverse to see if it is unique
        Public Function isUniqueInSV(ByVal attrName As String, attrVal As String) As Boolean

            Dim returnVal As Boolean = False

            Dim command As New SqlCommand
            Dim result As String
            command.Connection = _sqlConnection
            command.CommandType = CommandType.Text
            command.CommandText = String.Format("SELECT {0} from tblShadowverse where {0} = '{1}'", attrName, attrVal)

            result = command.ExecuteScalar()

            If result Is Nothing Then
                returnVal = True
                'Else
                '    If isMe(mailAlias, usercode) Then
                '        returnVal = True
                '    End If
            End If

            ', UIDName As String, UIDVal As String

            Return returnVal

        End Function

        Public Function getEntitlementsByRole(ByVal role As String) As List(Of String)

            Dim colNames As New List(Of String)
            Dim returnVals As New List(Of String)
            Dim result As Boolean

            Using _sqlConnection
                'First get the column names in case new ones have been added
                Dim queryString As String
                queryString = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tblEntitlements' and DATA_TYPE = 'BIT'"
                Dim command As New SqlCommand(queryString, _sqlConnection)
                Dim reader As SqlDataReader = command.ExecuteReader()
                While reader.Read
                    colNames.Add(reader(0))
                End While
                command.Dispose()
                reader.Close()

                'Now iterate through the columns and check the values
                For Each name As String In colNames

                    command.Connection = _sqlConnection
                    command.CommandType = CommandType.Text
                    command.CommandText = String.Format("SELECT {0} from tblEntitlements where {1} = '{2}'", name, "primaryRole", role)
                    result = command.ExecuteScalar()
                    If result Then
                        returnVals.Add(name)
                    End If
                Next


            End Using

            Return returnVals
        End Function

    End Class

End Class
